import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class GUI implements SwingConstants{
	
	// FRAME
	public JFrame frame = new JFrame("COMP 354");
	
	// MENU
	public JMenuBar menuBar = new JMenuBar();
	public JMenu mainMenu = new JMenu("Menu");
	public JMenuItem menuLogin = new JMenuItem("Login");
	public JMenuItem menuLogout = new JMenuItem("Logout");
	public JMenuItem menuCreateUser = new JMenuItem("Create User");
	public JMenuItem menuLoadDeck = new JMenuItem("Load Deck");
	
	// PANELS
	public JPanel currentDisplayPanel;
	public JPanel questionPanel = new JPanel(new GridLayout(0, 1));
	public JPanel loginPanel = new JPanel(new GridLayout(0, 1));
	public JPanel messagePanel = new JPanel(new GridLayout(0, 1));
	
	// LABELS
	public JLabel questionLabel = new JLabel("Question?");
	
	//FIELDS
	JPasswordField 	passwordField = new JPasswordField(10);
	JTextField 		usernameField = new JTextField(10);
	
	// BUTTONS
    JButton enterButton = new JButton("Enter");
    JButton backButton = new JButton("Back");
	
	// RADIO BUTTONS
	public JRadioButton[] answers = new JRadioButton[4];
	public ButtonGroup answerGroup = new ButtonGroup();
	
	
	public GUI(ActionListener register){
				
		frame.setSize(300,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// MENU
		frame.setJMenuBar(menuBar);

		menuBar.add(mainMenu);

		menuLogin.setActionCommand("menuLogin");
		menuLogout.setActionCommand("menuLogout");
		menuCreateUser.setActionCommand("menuCreateUser");
		menuLoadDeck.setActionCommand("menuLoadDeck");
		
		menuLogin.addActionListener(register);
		menuLogout.addActionListener(register);
		menuCreateUser.addActionListener(register);
		menuLoadDeck.addActionListener(register);
		
		mainMenu.add(menuLogin);
		mainMenu.add(menuLogout);
		mainMenu.add(menuCreateUser);
		mainMenu.add(menuLoadDeck);

		// QUESTION PANE
		questionLabel.setHorizontalAlignment(CENTER);
		
		questionLabel.setText("");
		questionPanel.add(questionLabel);

		for(int i = 0; i < 4; i++){
			answers[i] = new JRadioButton("");
			answers[i].setActionCommand("");
			answerGroup.add(answers[i]);
			questionPanel.add(answers[i]);
			answers[i].addActionListener(register);
		}
		
		// LOGIN PANE
		passwordField.setActionCommand("Enter");
        passwordField.addActionListener(register);
        usernameField.addActionListener(register);
        usernameField.setText("Enter your name here");
		
	    enterButton.setActionCommand("Enter");
	    backButton.setActionCommand("Back");
	    enterButton.addActionListener(register);
	    backButton.addActionListener(register);
	    
	    loginPanel.add(usernameField);
	    loginPanel.add(passwordField);
	    loginPanel.add(enterButton);
	    loginPanel.add(backButton);
		
	    // MESSAGE PANE
	    
	    
		// LOAD DEFAULT PANE
	    currentDisplayPanel = loginPanel;
		frame.add(currentDisplayPanel);
		
		frame.setVisible(true);
	}
	
	public void switchPane(JPanel pane){
		if(!currentDisplayPanel.equals(pane)){
			frame.add(pane);
			frame.remove(currentDisplayPanel);
			currentDisplayPanel = pane;
			frame.repaint();
		}
	}
	
		
	public void loadQuestion(Node question){
		questionLabel.setText(question.phrase);
		int i = 0;
		for(Node answer: question.children){
			answers[i].setText(answer.phrase);
			answers[i].setActionCommand(answer.phrase);
			i++;
		}
	}
	
}
